{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "qpath = r'../../' # path to quetzal here\n",
    "data = r'inputs/'\n",
    "import sys\n",
    "sys.path.insert(0, qpath)\n",
    "\n",
    "# import class\n",
    "from quetzal.io.gtfs_reader import importer"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Read GTFS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "feed = importer.GtfsImporter(path=data + r'bilbao.zip', dist_units='m')\n",
    "feed = feed.clean()\n",
    "feed.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Frequency conversion currently work only for one specific service and date, and on one given time period.  \n",
    "It computes the average headway over this time period."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Restrict to one date and merge services"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "feed = feed.restrict(dates=['20191210'])\n",
    "feed.group_services()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "feed.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "feed = feed.create_shapes()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "feed.map_trips(\n",
    "    feed.trips.groupby('route_id').first().trip_id.head(50)  # Map the first trip of each route\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Build patterns\n",
    "In a non-frequency based GTFS, a route has many trips, each corresponding to a start at a specific time. It is necessary to group these trips in order to compute their headway. A group of trips is refered as a pattern.\n",
    "The default method to build the patterns is by grouping trips based on their ordered list of stop_ids, without knowledge of time or duration.  \n",
    "Other methods to build the patterns are available: by parent stations, or clusters. See example 4-advanced-patterns.ipynb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "feed.build_patterns()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "feed.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Convert to frequencies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "time_range = ['06:00:00', '09:00:00']  # time format must be HH:MM:SS\n",
    "feed_f = feed.convert_to_frequencies(time_range=time_range)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "feed_f.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The average headway computed is the interval length divided by the number of trip starts within the interval.\n",
    "- a trip starting exactly at the start of the time range (6:00:00) is taken into account\n",
    "- a trip starting exactly at the end of the time range (9:00:00) is not considered."
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda env:quetzal_env_c]",
   "language": "python",
   "name": "conda-env-quetzal_env_c-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
